package cn.bgs.controller;

import cn.bgs.pojo.Grade;
import cn.bgs.service.GradeService;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.security.sasl.SaslServer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GradeController {

    Logger log= LogManager.getLogger();
    @Autowired private GradeService gradeService;

    @RequestMapping("/findAllGrade")
    public String findAllGrade(Model m,@RequestParam(defaultValue = "1")Integer pageNum){

        Integer pageSiz=3;

        PageInfo<Grade> list=gradeService.findAll(pageNum,pageSiz);

        m.addAttribute("p",list);

        return "/grade/grade";

    }

    @RequestMapping("/deleteGrade")
    public String deleteGrade(Integer pkId){

        gradeService.deleteGrade(pkId);

        return "redirect:/findAllGrade";

    }

    @RequestMapping("/updateByIdGrade")
    public String updateByIdGrade(Integer pkId,Model m){
            Grade grade= gradeService.selectByPrimaryKey(pkId);

            m.addAttribute("list",grade);
            return "/grade/gradeModify";

    }
    @RequestMapping("/updateGrade")
    public String updateGrade(Grade record){
        int ff=gradeService.updateByPrimaryKeySelective(record);

        return "redirect:/findAllGrade";

    }
    @RequestMapping("/gradeQuery")
    public String gradeQuery(Grade grade,Model m,@RequestParam(defaultValue = "1")Integer pageNum){


        Integer pageSiz=100;

        PageInfo<Grade> list=gradeService.gradeQuery(pageSiz,pageNum,grade);

        m.addAttribute("p",list);

        return "/grade/grade";

    }

    @RequestMapping("/danGrade")
    public String danGrade(Integer pkId,Model m){

      List<Grade> list= (List<Grade>) gradeService.selectByPrimaryKey(pkId);

        m.addAttribute("list",list);
        return "/grade/gradeDan";

    }








    @RequestMapping("/quanchas") /*Layui全查*/
    @ResponseBody
    public Map<String,Object> quanchas(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit){

        Map<String,Object> m=new HashMap<>();
        List<Grade> list=gradeService.findAllLayui();
        int total =list.size();
        if((limit*(page-1)+limit) >= total){
            m.put("data",list.subList(limit*(page-1),total));
        }else {
            m.put("data",list.subList(limit*(page-1),(limit*(page-1))+limit));
        }
        m.put("code","0000");
        m.put("msg","查询成功");
        m.put("count",total);
        return m;

    }





}
